package com.xygz.orm.utils;

import com.xiyuan.smartutils.StrUtils;
import com.xygz.orm.config.ORMConfig;

/**
 * the description for class SQLBuildUtils
 *
 * @author xiyuan-lgz 2024-01-02 @version v1.0.0 新建与整理
 */

@SuppressWarnings("all")
public class SQLBuildUtils {
    
    public static final String getTableName(String table, ORMConfig cfg) {
        if (cfg == null || !cfg.isCamelCase()) return table;
        return cfg.getTablePrefix() + toCamalCaseSQL(table, cfg);
    }
    
    public static final String toCamalCaseSQL(String segsql, ORMConfig cfg) {
        if (cfg == null || !cfg.isCamelCase()) return segsql;
        StringBuilder strb = new StringBuilder(segsql);
        int p = 0, c = 0;
        boolean isUppercase = cfg.isUpperSQL();
        for (int i = 0; i < strb.length(); i++, p = c) {
            c = strb.charAt(i);
            if (p != 0 && 65 <= c && c <= 90 && 97 <= p && p <= 122) {
                if (!isUppercase) strb.setCharAt(i, (char) (c + 32));
                strb.insert(i, '_');
                c = '_';
            }
            
            if (isUppercase && 97 <= c && c <= 122) {strb.setCharAt(i, (char) (c - 32));}
        }
        return strb.toString();
    }
}
